package com.alibaba.citrus.ots.platform.enhance.facade.service.employee;

import com.alibaba.citrus.ots.common.exception.FacadeException;
import com.alibaba.cz.base.tool.StringUtil;
import com.alibaba.cz.base.tool.message.ErrorMessage;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.model.dto.BaselineGetEmployeesByRelationOrgId2Request;
import com.epoch.app.bcots.model.dto.BaselineGetEmployeesByRelationOrgIds2Request;
import com.epoch.app.bcots.model.dto.EmployeeSDO;
import com.epoch.app.bcots.model.dto.GetEmployeesByRoleCodeRequest;
import com.epoch.app.bcots.model.dto.OmsListEmployeeAtPage2Request;
import com.epoch.app.bcots.service.BaseDataService;
import com.epoch.app.bcots.service.BcOtsService;
import com.epoch.app.otsplatformenhance.api.employeequery.dto.GetAllEmployeesByOrgIdAndRoleIdRequest;
import com.epoch.app.otsplatformenhance.api.employeequery.dto.GetEmployeesByOrgIdAndRoleIdEsRequest;
import com.epoch.app.otsplatformenhance.api.employeequery.dto.GetEmployeesByOrgIdAndRoleIdRequest;
import com.epoch.app.otsplatformenhance.api.employeequery.service.EmployeeQueryService;
import com.epoch.app.otsplatformenhance.api.organizationquery.service.OrganizationQueryService;
import com.epoch.app.otsplatformenhance.dto.request.IdQueryRequest;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/11/24 0:11
 */
@Component
@Primary
public class EmployeeQueryServiceImpl implements EmployeeQueryService {

    @Resource
    BaseDataService baseDataService;

    @Resource
    BcOtsService bcOtsService;

    @Resource
    OrganizationQueryService organizationQueryService;

    @Override
    public Result<List<EmployeeSDO>> getEmployeesByOrgIdAndRoleId(GetEmployeesByOrgIdAndRoleIdRequest request) {
        List<EmployeeSDO> result = new ArrayList<>();
        List<EmployeeSDO> roleEmployeeList = new ArrayList<>();
        List<EmployeeSDO> orgEmployeeList = new ArrayList<>();

        if(StringUtil.isNotEmpty(request.getOrganizationId())){
            BaselineGetEmployeesByRelationOrgId2Request getEmployeesByOrgIdRequest = new BaselineGetEmployeesByRelationOrgId2Request();
            getEmployeesByOrgIdRequest.setId(request.getOrganizationId());
            Result<List<EmployeeSDO>> orgEmployeeResult = baseDataService.baselineGetEmployeesByRelationOrgId2(getEmployeesByOrgIdRequest);
            orgEmployeeList = orgEmployeeResult.getResult();
        }
        if(StringUtil.isNotEmpty(request.getRoleCode())){
            for(int start=0; start<=1000; start+=100){
                GetEmployeesByRoleCodeRequest getEmployeesByRoleCodeRequest = new GetEmployeesByRoleCodeRequest();
                getEmployeesByRoleCodeRequest.setCode(request.getRoleCode());
                getEmployeesByRoleCodeRequest.setStart(start);
                getEmployeesByRoleCodeRequest.setSize(100);
                Result<List<EmployeeSDO>> roleEmployeeResult = baseDataService.getEmployeesByRoleCode(getEmployeesByRoleCodeRequest);
                roleEmployeeList.addAll(roleEmployeeResult.getResult());
                if(CollectionUtils.isEmpty(roleEmployeeResult.getResult())){
                    break;
                }
            }
        }
        if(StringUtil.isBlank(request.getRoleCode()) || StringUtil.isBlank(request.getOrganizationId())){
            result.addAll(roleEmployeeList);
            result.addAll(orgEmployeeList);
            return Result.listSuccess(result, result.size());
        }

        if(CollectionUtils.isEmpty(roleEmployeeList) || CollectionUtils.isEmpty(orgEmployeeList)){
            return Result.listSuccess(result, result.size());
        }

        Map<String,EmployeeSDO> roleEmployeeMap = roleEmployeeList.stream().collect(Collectors.toMap(EmployeeSDO::getId, Function.identity(),(v1, v2)->v1));
        result = orgEmployeeList.stream().filter(x -> roleEmployeeMap.get(x.getId()) != null ).collect(Collectors.toList());
        System.out.println(result.size());

        return Result.listSuccess(result, result.size());
    }

    @Override
    public Result<List<EmployeeSDO>> getEmployeesByOrgIdAndRoleIdEs(GetEmployeesByOrgIdAndRoleIdEsRequest getEmployeesByOrgIdAndRoleIdEsRequest) {
        if(StringUtil.isBlank(getEmployeesByOrgIdAndRoleIdEsRequest.getOrganizationId())){
            throw new FacadeException(ErrorMessage.code("OTS-01-003-01-15-028"));
        }
        OmsListEmployeeAtPage2Request request = new OmsListEmployeeAtPage2Request();
        request.setRelationOrganizationIdList(Lists.newArrayList(getEmployeesByOrgIdAndRoleIdEsRequest.getOrganizationId()));
        request.setLimit(1000);
        Result<List<EmployeeSDO>> queryResult = bcOtsService.omsListEmployeeAtPage2(request);
        if(StringUtil.isBlank(getEmployeesByOrgIdAndRoleIdEsRequest.getRoleCode())){
            return queryResult;
        }
        List<EmployeeSDO> result = queryResult.getResult().stream().filter(x -> x.getRoleList()
                .stream().anyMatch(y -> getEmployeesByOrgIdAndRoleIdEsRequest.getRoleCode().equals(y.getCode()))).collect(Collectors.toList());

        return Result.listSuccess(result,result.size());
    }

    @Override
    public Result<List<EmployeeSDO>> getAllEmployeesByOrgIdAndRoleId(GetAllEmployeesByOrgIdAndRoleIdRequest request) {
        if(StringUtil.isBlank(request.getRoleCode()) || StringUtil.isBlank(request.getOrganizationId())){
            return Result.listSuccess(Lists.newArrayListWithCapacity(0), 0);
        }
        List<EmployeeSDO> roleEmployeeList = new ArrayList<>();
        List<String> orgIds = new ArrayList<>();

        // 包括当前组织在内的所有子组织id
        IdQueryRequest idQueryRequest = new IdQueryRequest();
        idQueryRequest.setId(request.getOrganizationId());
        Result<List<com.epoch.app.otsplatformenhance.sdo.OrganizationSDO>> organizationList = organizationQueryService.getAllChildOrganizationList(idQueryRequest);
        if (CollectionUtils.isNotEmpty(organizationList.getResult())) {
            organizationList.getResult().forEach(org -> orgIds.add(org.getId()));
        }
        // 批量查询关联组织下的员工
        BaselineGetEmployeesByRelationOrgIds2Request getEmployeesByOrgIdRequest = new BaselineGetEmployeesByRelationOrgIds2Request();
            getEmployeesByOrgIdRequest.setIds(orgIds);
        Result<List<EmployeeSDO>> listResult = baseDataService.baselineGetEmployeesByRelationOrgIds2(getEmployeesByOrgIdRequest);
        if (CollectionUtils.isEmpty(listResult.getResult())) {
            return Result.listSuccess(Lists.newArrayListWithCapacity(0), 0);
        }
        for(int start=0; start<=1000; start+=100){
            GetEmployeesByRoleCodeRequest getEmployeesByRoleCodeRequest = new GetEmployeesByRoleCodeRequest();
            getEmployeesByRoleCodeRequest.setCode(request.getRoleCode());
            getEmployeesByRoleCodeRequest.setStart(start);
            getEmployeesByRoleCodeRequest.setSize(100);
            Result<List<EmployeeSDO>> roleEmployeeResult = baseDataService.getEmployeesByRoleCode(getEmployeesByRoleCodeRequest);
            roleEmployeeList.addAll(roleEmployeeResult.getResult());
            if(CollectionUtils.isEmpty(roleEmployeeResult.getResult())) {
                break;
            }
        }
        if(CollectionUtils.isEmpty(roleEmployeeList)){
            return Result.listSuccess(Lists.newArrayListWithCapacity(0), 0);
        }
        Map<String,EmployeeSDO> roleEmployeeMap = roleEmployeeList.stream().collect(Collectors.toMap(EmployeeSDO::getId, Function.identity(),(v1, v2)->v1));
        List<EmployeeSDO> sdoList = listResult.getResult().stream().filter(x -> roleEmployeeMap.get(x.getId()) != null).collect(Collectors.toList());
        return Result.listSuccess(sdoList, sdoList.size());
    }
}
